/**
 * Desc :
 * Created by byte2 on 2016-12-09.
 * ref :https://segmentfault.com/a/1190000005128101
 */
import "./less/less/rsuite.less";
import "./rsuite/table/less/style.less";
import "./less/css.css";
import "./less/css.less";
import "./less/add.css";
import CONFIG from "./libs/config.js";
import "./libs/polyfill.js";
// import 'babel-runtime/core-js/promise'; //
import "fetch-ie8";
import "es5-shim";
import "es5-shim/es5-sham";
import 'babel-polyfill';
import 'console-polyfill';

import {
    NotFound,
    HomeworkDetail,
    Profile,
    Reset,
    AddHomework,
    Duty,
    Homework,
    DetailDayNews,
    AddDayNews,
    DayNews,
    Templates,
    AddTeachSchedule,
    TeachSchedule,
    UpdateTeachSchedule,
    DetailTeachSchedule,
    AddClassStudent,
    ClassStudent,
    ClassTeacher,
    ClassTeacherDetail,
    Schedule,
    ClassPhotos,
    ClassPhotosDetail,
    Notices,
    NoticeAdd,
    NoticeDetail,
    NoticesCategory,
    ClassProfile,
    ClassHonour,
    ClassHonourDetail,
    AddClassHonour,
    NewsDetail,
    Category,
    Activity,
    AddActivity,
    Feedback,
    DetailActivity,
    ActivityCategory,
    Login,
    Home,
    App,
    AddNews,
    News,
} from './containers/index';

import reducers from './reducers/index.js';

import {createStore, combineReducers, applyMiddleware} from 'redux';
import {Router, Route, IndexRoute, browserHistory} from "react-router";
import {syncHistoryWithStore, routerReducer, routerActions, routerMiddleware} from 'react-router-redux';
import {Provider} from 'react-redux';
import createLogger from 'redux-logger';
import thunkMiddleware from 'redux-thunk';
import {UserAuthWrapper} from 'redux-auth-wrapper';


const routingMiddleware = routerMiddleware(browserHistory);
const middlewares = [thunkMiddleware, routingMiddleware];


if (process.env.NODE_ENV === 'development') {
    const logger = createLogger();
    middlewares.push(logger);
}

let store = createStore(
    combineReducers({
        routing: routerReducer,
        ...reducers
    }),
    applyMiddleware(...middlewares)
);

const history = syncHistoryWithStore(browserHistory, store);

// const Authenticated = UserIsAuthenticated((props) => {
//     return React.cloneElement(props.children, props);
// });

//user login auths
const UserIsAuthenticated = UserAuthWrapper({
    authSelector: (state) => {
        return state.user && state.user.user;
    },
    failureRedirectPath: '/login',
    redirectAction: routerActions.replace,
    wrapperDisplayName: 'UserIsAuthenticated'
});
// const Authenticated = UserIsAuthenticated((props) => React.cloneElement(props.children, props));

ReactDOM.render(
    (
        <Provider store={store}>
            <Router history={history}>
                <Route path="/login" component={Login}/>
                <Route path="/" component={UserIsAuthenticated(App)}>
                    <IndexRoute component={Home}/>
                    <Route path={CONFIG.MENUS.HOME} component={Home}/>
                    <Route path={CONFIG.MENUS.NEWS} component={News}></Route>
                    <Route path={CONFIG.MENUS.NEWS_ADD} component={AddNews}/>
                    <Route path={CONFIG.MENUS.NEWS_DETAIL} component={NewsDetail}/>
                    <Route path={CONFIG.MENUS.NEWS_UPDATE} component={AddNews}/>
                    <Route path={CONFIG.MENUS.NEWS_CATEGORY} component={Category}></Route>

                    <Route path={CONFIG.MENUS.ACTIVITIES} component={Activity}></Route>
                    <Route path={CONFIG.MENUS.ACTIVITIES_ADD} component={AddActivity}></Route>
                    <Route path={CONFIG.MENUS.ACTIVITIES_CATEGORY} component={ActivityCategory}></Route>
                    <Route path={CONFIG.MENUS.ACTIVITIES_UPDATE} component={AddActivity}></Route>
                    <Route path={CONFIG.MENUS.ACTIVITIES_DETAIL} component={DetailActivity}></Route>

                    <Route path={CONFIG.MENUS.NOTICES} component={Notices}></Route>

                    <Route path={CONFIG.MENUS.NOTICES_ADD} component={NoticeAdd}></Route>
                    <Route path={CONFIG.MENUS.NOTICES_UPDATE} component={NoticeAdd}></Route>
                    <Route path={CONFIG.MENUS.NOTICES_DETAIL} component={NoticeDetail}></Route>
                    <Route path={CONFIG.MENUS.NOTICES_CATEGORY} component={NoticesCategory}></Route>

                    <Route path={CONFIG.MENUS.CLASS_PROFILE} component={ClassProfile}></Route>
                    <Route path={CONFIG.MENUS.CLASS_HONOUR} component={ClassHonour}></Route>
                    <Route path={CONFIG.MENUS.CLASS_HONOUR_ADD} component={AddClassHonour}></Route>
                    <Route path={CONFIG.MENUS.CLASS_HONOUR_UPDATE} component={AddClassHonour}></Route>
                    <Route path={CONFIG.MENUS.CLASS_HONOUR_DETAIL} component={ClassHonourDetail}></Route>

                    <Route path={CONFIG.MENUS.CLASS_PHOTOS} component={ClassPhotos}></Route>
                    <Route path={CONFIG.MENUS.CLASS_PHOTOS_DETAIL} component={ClassPhotosDetail}></Route>
                    <Route path={CONFIG.MENUS.SCHEDULE} component={Schedule}></Route>
                    <Route path={CONFIG.MENUS.CLASS_STUDENTS} component={ClassStudent}></Route>
                    <Route path={CONFIG.MENUS.CLASS_STUDENT_ADD} component={AddClassStudent}></Route>
                    <Route path={CONFIG.MENUS.CLASS_STUDENT_UPDATE} component={AddClassStudent}></Route>
                    <Route path={CONFIG.MENUS.CLASS_TEACHERS} component={ClassTeacher}></Route>
                    <Route path={CONFIG.MENUS.CLASS_TEACHERS_DETAIL} component={ClassTeacherDetail}></Route>
                    <Route path={CONFIG.MENUS.HOMEWORK} component={Homework}></Route>
                    <Route path={CONFIG.MENUS.HOMEWORK_ADD} component={AddHomework}></Route>
                    <Route path={CONFIG.MENUS.HOMEWORK_UPDATE} component={AddHomework}></Route>
                    <Route path={CONFIG.MENUS.HOMEWORK_DETAIL} component={HomeworkDetail}></Route>

                    <Route path={CONFIG.MENUS.DAY_NEWS} component={DayNews}></Route>
                    <Route path={CONFIG.MENUS.DAY_NEWS_ADD} component={AddDayNews}></Route>
                    <Route path={CONFIG.MENUS.DAY_NEWS_UPDATE} component={AddDayNews}></Route>
                    <Route path={CONFIG.MENUS.DAY_NEWS_DETAIL} component={DetailDayNews}></Route>

                    <Route path={CONFIG.MENUS.TEACH_SCHEDULE} component={TeachSchedule}></Route>
                    <Route path={CONFIG.MENUS.TEACH_SCHEDULE_ADD} component={AddTeachSchedule}></Route>
                    <Route path={CONFIG.MENUS.TEACH_SCHEDULE_UPDATE} component={AddTeachSchedule}></Route>
                    <Route path={CONFIG.MENUS.TEACH_SCHEDULE_DETAIL} component={DetailTeachSchedule}></Route>
                    <Route path={CONFIG.MENUS.DUTY} component={Duty}></Route>
                    <Route path={CONFIG.MENUS.TEMPLATES} component={Templates}></Route>

                    <Route path={CONFIG.MENUS.PROFILE} component={Profile}></Route>
                    <Route path={CONFIG.MENUS.RESET} component={Reset}></Route>
                    <Route path={CONFIG.MENUS.FEEDBACK} component={Feedback}></Route>
                </Route>
                <Route path="*" component={NotFound}/>
            </Router>
        </Provider>
    ),
    document.getElementById('content_')
);
